import Vue from 'vue'
import Router from 'vue-router'
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'
import AddBlog from '../views/AddBlog.vue'
import EditorBlog from '../views/EditorBlog.vue'
import ShowBlog from '../views/ShowBlog.vue'
import SearchBlog from '../views/SearchBlog.vue'
import HomeTable from '../components/HomeTable.vue'
import { setCookie,setCookie2, getCookie, checkCookie, clearCookie } from '@/utils/cookie';

Vue.use(Router)
const VueRouterPush = Router.prototype.push
Router.prototype.push = function push (to) {
  return VueRouterPush.call(this, to).catch(err => err)
}

Vue.prototype.$setCookie = setCookie;
Vue.prototype.$setCookie2 = setCookie2;
Vue.prototype.$getCookie = getCookie;
Vue.prototype.$checkCookie = checkCookie;
Vue.prototype.$clearCookie = clearCookie;

var axios = require('axios')
Vue.prototype.$axios = axios


const router = new Router({
  // history: createWebHistory(process.env.BASE_URL),
  mode:'hash',
  routes: [
    // {
    //   path: '/',
    //   name: 'Login',
    //   component: Login
    // },
    {
      path: '/',
      name: 'HomeTable',
      component: HomeTable,
      children: [{
        path: '/home',
        component: Home
      },
      {
        path: '/addBlog',
        component: AddBlog
      },
      {
        path: '/searchBlog',
        component: SearchBlog
      },
    ]
    },
    {
      path: '/showBlog',
      name: 'ShowBlog',
      component: ShowBlog
    },
    {
      path: '/editorBlog',
      name: 'EditorBlog',
      component: EditorBlog
    },
  ]
})

// router.beforeEach((to,from,next)=>{
//   var loginToken = getCookie("HBlogToken");
//   console.log(loginToken);
//   var formdata = new FormData();
//       formdata.append("loginToken",loginToken);
//   axios
//   ({
//     method:"post",
//     url:"http://120.77.71.211:9011/api/isAutoLogin",
//     data: formdata,
//     headers: {
//       "content-type": "multipart/form-data",
//     },
//   })
//     .then((res) => {
//       if(to.name == 'Login' && res.data.code==200) next({name:'HomeTable'})
//       else next()
//     });
// })

export default router
